package com.glodon.paas.activity.dao.mysql;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Autowired;

import com.glodon.paas.activity.dao.ActivityDefinitionDao;
import com.glodon.paas.activity.dao.domain.ActivityDefinitionEntity;
import com.glodon.paas.activity.dao.mapper.ActivityDefinitionEntityMapper;
import com.glodon.paas.exception.PaasServerExceptionFactory;

public class ActivityDefinitionDaoMysqlImpl implements ActivityDefinitionDao {

    private Map<Long, ActivityDefinitionEntity> definitionCache;
    private List<ActivityDefinitionEntity>      allCache;

    long                                        cacheTime   = 0;
    long                                        cachePeriod = 120 * 1000;

    @Autowired
    private ActivityDefinitionEntityMapper      activityDefinitionEntityMapper;

    @Override
    public Long insertSelective(ActivityDefinitionEntity record) {
        if (record.getActivityType() == null) {
            throw PaasServerExceptionFactory.validateException("id is null", null);
        }
        activityDefinitionEntityMapper.insertSelective(record);
        definitionCache = null;
        allCache = null;
        return record.getActivityType();
    }

    @Override
    public int deleteByPrimaryKey(Long activityType) {
        int rt =  activityDefinitionEntityMapper.deleteByPrimaryKey(activityType);
        definitionCache = null;
        allCache = null;
        return rt;
    }

    @Override
    public int updateByPrimaryKeySelective(ActivityDefinitionEntity record) {

        ActivityDefinitionEntity recordDb = selectByPrimaryKey(record.getActivityType());
        if (recordDb == null) {
            return 0;
        }
        record.setCreateTime(recordDb.getCreateTime());
        record.setUpdateTime(new Date());
        
        int rt = activityDefinitionEntityMapper.updateByPrimaryKeySelective(record);
        definitionCache = null;
        allCache = null;
        return rt;
    }

    @Override
    public ActivityDefinitionEntity selectByPrimaryKey(Long activityType) {
        if(definitionCache == null){
            reflashCache();
        }
        //return activityDefinitionEntityMapper.selectByPrimaryKey(activityType);
        return definitionCache.get(activityType);
    }

    @Override
    public List<ActivityDefinitionEntity> selectByAppId(Integer appId) {
        if (appId == null) {
            return new ArrayList<ActivityDefinitionEntity>();
        }
        
        List<ActivityDefinitionEntity> all = allCache;
        if(all == null){
            all = selectAll();
            allCache = all;
        }
        
        List<ActivityDefinitionEntity> list = new ArrayList<ActivityDefinitionEntity>();
        for(ActivityDefinitionEntity item : all){
            if(appId.equals(item.getAppId())){
                list.add(item);
            }
        }
        return list;
    }

    @Override
    public List<ActivityDefinitionEntity> selectAll() {
        return activityDefinitionEntityMapper.selectAll();
    }

    public void reflashCache() {
        List<ActivityDefinitionEntity> list = selectAll();
        allCache = list;
        
        Map<Long, ActivityDefinitionEntity> temp = new HashMap<Long, ActivityDefinitionEntity>();
        for (ActivityDefinitionEntity def : list) {
            temp.put(def.getActivityType(), def);
        }
        definitionCache = temp;
    }
    
    public ActivityDefinitionEntity getByActivityType(Long activityType) {
        long currentTime = System.currentTimeMillis();
        if (definitionCache == null || (currentTime - cacheTime > cachePeriod)) {
            Map<Long, ActivityDefinitionEntity> temp = new HashMap<Long, ActivityDefinitionEntity>();
            List<ActivityDefinitionEntity> list = this.selectAll();
            for (ActivityDefinitionEntity def : list) {
                temp.put(def.getActivityType(), def);
            }
            definitionCache = temp;
            cacheTime = currentTime;
        }

        return definitionCache.get(activityType);
    }
}
